import holiday from './holiday'
import moment from 'moment'

/**
 * 根据传入的字符串获取小时和分钟
 * @param {String} time  //'05:40'
 * @returns {Array}  [hour,minute]  // [ 5, 40 ]
 */
export const getHourAndMinute = time => {
  const tmpArr = time.split(':')
  if (tmpArr.length !== 2) return [null, null]
  return [parseInt(tmpArr[0]), parseInt(tmpArr[1])]
}

/**
 * 获取配置信息
 * @returns {Object}  配置信息
 */
export const getConfig = () => {
  const config = JSON.parse(localStorage.getItem('goHomeConfig') || '{}')
  //默认配置
  const defaultConfig = {
    onWorkTime: '09:00', //上班时间
    offWorkTime: '18:00', //下班时间
    orderWarnTime: '', //点餐提醒时间
    workType: 0, //工作制，0双休 1大小周 2单休
    payOffDay: 10 //发薪日
  }
  return Object.assign(defaultConfig, config)
}

/**
 * 获取当天是否需要上班
 * @returns {Boolean}  是否需要上班
 */
export const getIsWorkDay = () => {
  const now = new moment()
  const date = now.format('YYYY-MM-DD') //当前日期
  const inWorkDay = holiday.some(item => item.workDay.some(item => item === date))
  const inRestDay = holiday.some(item => item.restDay.some(item => item === date))
  if (inWorkDay) {
    return true
  }
  if (inRestDay) {
    return false
  }
  return now.format('d') > 0 && now.format('d') < 6
}
